window.addEventListener('load',function(){
    var arrowl = document.querySelector('.arrow-l');
    var arrowr = document.querySelector('.arrow-r');
    var focus = document.querySelector('.focus');
    //鼠标经过显示左右按钮
    focus.addEventListener('mouseenter',function(){
        arrowl.style.display = 'block';
        arrowr.style.display = 'block';
        clearInterval(timer);
        timer = null;
    })
    focus.addEventListener('mouseleave',function(){
        // clearInterval(timer);
        arrowl.style.display = 'none';
        arrowr.style.display = 'none';
        timer = setInterval(function(){
            arrowr.click()
        },2000)
    })
    //动态添加小圆圈,同时添加点击事件
    var ul = focus.querySelector('ul');
    var ol = focus.querySelector('.circle');
    var num = 0;
    var circle = 0;
    for(var i = 0;i<ul.children.length;i++){
        var li =document.createElement('li');
        li.setAttribute('index',i);
        ol.appendChild(li);
        //排他思想
        li.addEventListener('click',function(){
            for(var i = 0;i <ul.children.length;i++){
                ol.children[i].className = '';
            }
            this.className = 'current';
            var index = this.getAttribute('index');
            num = index;
            circle = index;
            animate(ul,-721*index);
        })
    }
    ol.children[0].className = 'current'; 
    var first = ul.children[0].cloneNode(true);
    ul.appendChild(first);
    var flag = true;
    //flag节流阀
    arrowr.addEventListener('click',function(){
        if(flag){
            flag = false;
            if(num == ul.children.length - 1){
                ul.style.left = '0';
                num = 0;
            }
            num++;
            animate(ul,num*-721,function(){
                flag = true;
            });
             circle++;
             circle === ol.children.length ? circle=0 :circle;
             circleChange();
        }
    })
    arrowl.addEventListener('click',function(){
        if(flag){
            flag = false;
            if(num == 0){
                ul.style.left = -(ul.children.length - 1)*721 + 'px';
                num = ul.children.length - 1;
            }
            num--;
            animate(ul,-num*721,function(){
                flag = true;
            });
             circle--;
             circle = circle < 0 ? ol.children.length-1 :circle;
             circleChange();
        }
    })
    function circleChange(){
        for(var i = 0;i <ol.children.length;i++){
            ol.children[i].className = '';
        }
        ol.children[circle].className = 'current'; 
    }
    var timer = setInterval(function(){
        arrowr.click()
    },2000)
})
